package com.fingard.dsp.bank.directbank.pab01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class PAB01Deposit extends PAB01Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            PAB01Header bankHeader = new PAB01Header();
            bankHeader.trncode = "4021";
            bankHeader.fourZero = "0000";
            bankHeader.reqSeqNo = getQryTransID();

            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("Result");
            xmlWriter.writeElementString("AcctNo", p_req7507.actNum);

            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

            if (tmpStrRet[0].length() == 0) {
                byte[] retBytes = tmpStrRet[1].getBytes(getCharset());
                String[] tmpRetArr = new String(retBytes, 87, 106, getCharset()).trim().split(":");
                String tmpRetCode = tmpRetArr[0];
                String tmpRetInfo = tmpRetArr[1];
                if ("000000".equals(tmpRetCode)) {
                    if (tmpStrRet[1].indexOf("<") >= 0) {
                        tmpStrRet[1] = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<"));
                    }
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        while (tmpBkReader.read("/Result/list")) {
                            XElement readXElement = tmpBkReader.readedXElement;
                            RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                            retAtsDetail.actNum = p_req7507.actNum;//银行账号
                            retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                            retAtsDetail.depositCode = readXElement.getSingleTextNotNull("DepositReceiptNo");//存单号
                            retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("CurrencyNo"));//币种
                            retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("Amount");//账户余额
                            retAtsDetail.rate = readXElement.getSingleTextNotNull("AcctOpenIntRate");//开户利率
                            retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("ValueDate")));//起息日
                            retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("ValueDate")));//起息日
                            retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("ExpiryDate")));//起息日
                            retAtsDetail.terms = "9";
                            retAts.writeRetDetail(retAtsDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpRetInfo;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
